import com.codezen.component.minibrowser.CzMiniBrowser;
import com.codezen.helper.Worker;

import flash.events.ErrorEvent;
import flash.events.Event;

import logic.SerialsDataLoader;

import mx.collections.ArrayCollection;
import mx.collections.Sort;
import mx.collections.SortField;
import mx.core.FlexGlobals;
import mx.managers.PopUpManager;
import mx.utils.ObjectUtil;

import utils.serials.Episode;
import utils.serials.Serial;

private var dl:SerialsDataLoader;

// data providers
[Bindable]
private var lastEpisodes:ArrayCollection;
[Bindable]
private var seriesList:ArrayCollection;
[Bindable]
private var iviewSeriesList:ArrayCollection;
[Bindable]
private var episodesList:ArrayCollection;
[Bindable]
private var seasonsList:ArrayCollection;

// seasons filter
private var showSeason:String = "1";

// old state
private var oldState:String;

// current episode
public var currentEpisode:Episode;

// current serial
public var currentSeries:Serial;

private function onLoginState():void{	
	// set login and pass
	if( ruhd ){
		login_input.text = login_hd;
		pass_input.text = pass_hd;
	}else{
		login_input.text = login_sd;
		pass_input.text = pass_sd;
	}
}

private function onMainState():void{
	// do nothing
}


//
private function tryLogin(isOffline:Boolean = false):void{
	// init database
	initDatabase();
	
	// load stuff
	loadSeriesList();
}

private function loadSeriesList():void{
	// serials
	if( rusd ){
		dl = new SerialsDataLoader(login_sd, pass_sd, "s");
		this.title = "RuSHDtv - RuSD.tv";
	}else if(ruhd){
		dl = new SerialsDataLoader(login_hd, pass_hd, "h");
		this.title = "RuSHDtv - RuHD.tv";
	}
	dl.addEventListener(ErrorEvent.ERROR, onAuthError);
	dl.addEventListener(Event.INIT, onAuthLoad);
}

private function onAuthLoad(evt:Event):void{
	// remove event listener
	dl.removeEventListener(Event.INIT, onAuthLoad);
	
	// change state
	currentState = "Main";
	
	// get news
	dl.addEventListener(Event.COMPLETE, onNewsLoad);
	dl.getNews();
}

private function onNewsLoad(e:Event):void{
	dl.removeEventListener(Event.COMPLETE, onNewsLoad);
	
	// init
	lastEpisodes = new ArrayCollection();
	
	// set new stuff
	var item:Episode;
	for each(item in dl.lastEpisodes){
		lastEpisodes.addItem(item);
	}
	
	// check eps from DB
	dbLoadNewEpisodes();
	
	// get series
	dl.addEventListener(Event.COMPLETE, onDataLoad);
	dl.requestData();
}


private function onDataLoad(evt:Event):void{
	// remove event listener
	dl.removeEventListener(Event.COMPLETE, onDataLoad);
	
	// init
	seriesList = new ArrayCollection();
	
	// set series
	var item:Serial;
	for each(item in dl.seriesList){
		seriesList.addItem(item);
	}
	
	// create sort by names
	var sort:Sort = new Sort();
	sort.fields = [new SortField("title")];
	
	// sort
	seriesList.sort = sort;
	seriesList.refresh();
	
	// load series from DB
	dbLoadSeries();
	
	// clean
	item = null;
	sort = null;
	
	// force GC
	startGCCycle();
	
	// load anime
	//loadAnimeList();
}

private function loadSeries(id:String):void{
	dl.addEventListener(Event.COMPLETE, onSeriesLoad);
	dl.loadSeries(id);
}

private function onSeriesLoad(e:Event):void{
	dl.removeEventListener(Event.COMPLETE, onSeriesLoad);
	
	// init
	episodesList = new ArrayCollection();
	
	// get eps
	var item:Episode;
	for each(item in dl.episodesList){
		episodesList.addItem(item);
	}
	
	var sort:Sort = new Sort();
	sort.fields = [new SortField("num", false, true, true)];
	episodesList.sort = sort;
	episodesList.refresh();
	
	// clean
	sort = null;
	item = null;
	
	// set seasons
	breakSeasons();
}

private function breakSeasons():void{
	seasonsList = new ArrayCollection();
	
	var tmp:Array = [];
	var item:Episode;
	for each(item in episodesList){
		if(tmp.lastIndexOf(item.season) < 0){
			tmp.push(item.season);
			seasonsList.addItem({label:"Сезон "+item.season,season:item.season});
		}
	}
	
	var sort:Sort = new Sort();
	sort.fields = [new SortField("season", false, true, true)];
	seasonsList.sort = sort;
	seasonsList.refresh();
	
	// clean
	sort = null;
	item = null;
	tmp = null;
	
	// force GC
	startGCCycle();
	
	// set index
	seasons_combobox.selectedIndex = 0;
	filterSeasons(null);
}

private function onAuthError(evt:Event):void{
	// remove event listener
	dl.removeEventListener(ErrorEvent.ERROR, onAuthError);
}